/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.DataStructures
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// SynchronizedList<T> class is an independent class 
    /// that can trun existing IList<T> object 
    /// into synchronized list or work 
    /// severally to project sysnchronized list 
    /// functionality.
    /// </summary>
    /// <typeparam name="T">The generic type.</typeparam>
    public class SynchronizedList<T> : IList<T>
    {
        private object _syncRoot;

        public SynchronizedList()
        {
            _innerList = new List<T>();
            _syncRoot = SyncRoot;
        }

        public SynchronizedList(IList<T> list)
        {
            _innerList = list;
            _syncRoot = SyncRoot;
        }

        public SynchronizedList(object syncRoot)
        {
            _innerList = new List<T>();
            _syncRoot = syncRoot;
        }

        public SynchronizedList(IList<T> list, object syncRoot)
        {
            _innerList = list;
            _syncRoot = syncRoot;
        }

        protected object SyncRoot
        {
            get
            {
                object syncRoot = null;
                if (syncRoot == null)
                {
                    object newRoot = new object();
                    System.Threading.Interlocked.CompareExchange(ref syncRoot, newRoot, null);
                }
                return syncRoot;
            }
        }

        private IList<T> _innerList;
        protected IList<T> InnerList
        {
            get { return _innerList; }
            set { _innerList = value; }
        }


        #region IList<T> Members

        public int IndexOf(T item)
        {
            lock (_syncRoot)
            {
                return InnerList.IndexOf(item);
            }
        }

        public void Insert(int index, T item)
        {
            lock (_syncRoot)
            {
                InnerList.Insert(index, item);
            }
        }

        public void RemoveAt(int index)
        {
            lock (_syncRoot)
            {
                InnerList.RemoveAt(index);
            }
        }

        public T this[int index]
        {
            get
            {
                lock (_syncRoot)
                {
                    return InnerList[index];
                }
            }
            set
            {
                lock (_syncRoot)
                {
                    InnerList[index] = value;
                }
            }
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            lock (_syncRoot)
            {
                InnerList.Add(item);
            }
        }

        public void Clear()
        {
            lock (_syncRoot)
            {
                InnerList.Clear();
            }
        }

        public bool Contains(T item)
        {
            lock (_syncRoot)
            {
                return InnerList.Contains(item);
            }
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            lock (_syncRoot)
            {
                InnerList.CopyTo(array, arrayIndex);
            }
        }

        public int Count
        {
            get
            {
                lock (_syncRoot)
                {
                    return InnerList.Count;
                }
            }
        }

        public bool IsReadOnly
        {
            get
            {
                lock (_syncRoot)
                {
                    return false;
                }
            }
        }

        public bool Remove(T item)
        {
            lock (_syncRoot)
            {
                return InnerList.Remove(item);
            }
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            lock (_syncRoot)
            {
                return InnerList.GetEnumerator();
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            lock (_syncRoot)
            {
                return GetEnumerator();
            }
        }

        #endregion

    }
}
